package com.wholeintech.dao;

import java.util.List;

import com.wholeintech.entity.SceneDevice;
import com.wholeintech.utils.DatabaseUtils;

import android.content.Context;

import org.xutils.DbManager;
import org.xutils.ex.DbException;

/**
 * 场景设备数据操作类
 *
 * @author Administrator
 */
public class SceneDeviceDao {
    private final static String TAG = "SceneDeviceDao";
    private Context mContext = null;

    public SceneDeviceDao(Context context) {
        this.mContext = context;
    }

    /**
     * 新增
     *
     * @param sceneDevice
     * @return
     */
    public boolean add(SceneDevice sceneDevice) {
        boolean bRet = false;
        DbManager db = DatabaseUtils.DeviceDbUtils();
        try {
            db.save(sceneDevice);
            bRet = true;
        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return bRet;
    }

    /**
     * 删除场景设备
     *
     * @param intSecenDeviceId
     */
    public void delete(int intSecenDeviceId) {
        DbManager db = DatabaseUtils.DeviceDbUtils();

        try {
            db.deleteById(SceneDevice.class, intSecenDeviceId);
        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * 删除场景设备
     */
    public void deleteAll() {
        DbManager db = DatabaseUtils.DeviceDbUtils();

        try {
            //db.deleteAll(SceneDevice.class);
            db.dropTable(SceneDevice.class);
        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * 获取指定场景设备
     *
     * @param sceneId
     * @return
     */
    public List<SceneDevice> getSceneDeviceBySceneId(String sceneId) {
        DbManager db = DatabaseUtils.DeviceDbUtils();
        List<SceneDevice> sceneList = null;

        try {
//            sceneList = db.findAll(Selector.from(SceneDevice.class).where("sceneId", "=", sceneId));
            sceneList = db.selector(SceneDevice.class).where("sceneId", "=", sceneId).findAll();
            if (sceneList != null) {
                sceneList.size();
            }

        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return sceneList;
    }
}
